package com.lft.com.oilgas.controller;


import com.lft.com.oilgas.entity.Commodity;
import com.lft.com.oilgas.util.QueryBean;
import com.lft.com.oilgas.util.Result;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.lft.com.oilgas.service.impl.CommodityServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author tyqw
 * @since 2024-09-10
 */
@RestController
@Transactional
@RequestMapping("/util/commodity")
public class CommodityController {
    @Autowired
    CommodityServiceImpl commodityService;

    @ExceptionHandler(Exception.class)
    public Result xx(Exception ex) {
        //noinspection CallToPrintStackTrace
        ex.printStackTrace();
        return new Result(false, "服务器异常，等会再试");
    }

    @RequestMapping("/insterupdateCommodity")
    public Result InsterOrupdateCommodity(@RequestBody Commodity commodity) {
        return new Result(true, commodityService.insterOrupdateCommodity(commodity));
    }

    @RequestMapping("/GetCommodityByQueryBean")
    public Result getCommodity(@RequestBody QueryBean queryBean) {
        return new Result(true, commodityService.getCommodities(queryBean));
    }

    //根据商店id查询商品
    @RequestMapping("/byid/{id}")
    public Result bymerchantid(@PathVariable("id") Integer id) {
        return new Result(true, commodityService.bymerchantid(id));
    }

    //修改商品
    @RequestMapping("/xiuorzheng")
    public Result xiuorzheng(@RequestBody Commodity commodity) {
        commodityService.insertOrUpdate(commodity);
        return new Result(true, "删除成功");
    }
    //根据id查询详情
    @RequestMapping("/xianqing/{id}")
    public Result selectById(@PathVariable Integer id){
        Commodity commodity = commodityService.selectByid(id);
        return new Result(true,commodity);
    }
    //根据商品id删除
    @RequestMapping("/delectsc/{id}")
    public  Result delectsc(@PathVariable Integer id){
        commodityService.delectById(id);
        return new Result(true,null);
    }
}
